[bits 32]

[extern isr_handler]
[extern irq_handler]

global isr0
global isr1
global isr2
global isr3
global isr4
global isr5
global isr6
global isr7
global isr8
global isr9
global isr10
global isr11
global isr12
global isr13
global isr14
global isr15
global isr16
global isr17
global isr18
global isr19
global isr20
global isr21
global isr22
global isr23
global isr24
global isr25
global isr26
global isr27
global isr28
global isr29
global isr30
global isr31

global irq0
global irq1
global irq2
global irq3
global irq4
global irq5
global irq6
global irq7
global irq8
global irq9
global irq10
global irq11
global irq12
global irq13
global irq14
global irq15

isr_common_stub:
    ; 1. 保存cpu状态
    pusha
    push ds
    push esp     ; 将栈顶地址作为isr_register_t* reg参数
    mov ax, 0x10 ; 内核数据段选择子
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    
    ; 2. 调用处理逻辑
    call isr_handler

    ; 3. 恢复cpu状态
    pop eax
    pop eax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    popa
    add esp, 8 ; 弹出中断号和错误码
    iret      ; 弹出cpu自动保存的寄存器

irq_common_stub:
    ; 1. 保存cpu状态
    pusha
    push ds
    push esp     ; 将栈顶地址作为isr_register_t* reg参数
    mov ax, 0x10 ; 内核数据段选择子
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    
    ; 2. 调用处理逻辑
    call irq_handler

    ; 3. 恢复cpu状态
    ; eax存放返回的esp 设置esp从而切换任务
    mov esp, eax
    ; pop eax
    pop eax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    popa
    add esp, 8 ; 弹出中断号和错误码
    iret      ; 弹出cpu自动保存的寄存器

isr0:
    push byte 0 ; 只是指令中的立即数是1字节 实际入栈还是4字节
    push byte 0
    jmp isr_common_stub
isr1:
    push byte 0
    push byte 1
    jmp isr_common_stub
isr2:
    push byte 0
    push byte 2
    jmp isr_common_stub
isr3:
    push byte 0
    push byte 3
    jmp isr_common_stub
isr4:
    push byte 0
    push byte 4
    jmp isr_common_stub
isr5:
    push byte 0
    push byte 5
    jmp isr_common_stub
isr6:
    push byte 0
    push byte 6
    jmp isr_common_stub
isr7:
    push byte 0
    push byte 7
    jmp isr_common_stub
isr8:
    push byte 0
    push byte 8
    jmp isr_common_stub
isr9:
    push byte 0
    push byte 9
    jmp isr_common_stub
isr10:
    push byte 0
    push byte 10
    jmp isr_common_stub
isr11:
    push byte 0
    push byte 11
    jmp isr_common_stub
isr12:
    push byte 0
    push byte 12
    jmp isr_common_stub
isr13:
    push byte 0
    push byte 13
    jmp isr_common_stub
isr14:
    push byte 0
    push byte 14
    jmp isr_common_stub
isr15:
    push byte 0
    push byte 15
    jmp isr_common_stub
isr16:
    push byte 0
    push byte 16
    jmp isr_common_stub
isr17:
    push byte 0
    push byte 17
    jmp isr_common_stub
isr18:
    push byte 0
    push byte 18
    jmp isr_common_stub
isr19:
    push byte 0
    push byte 19
    jmp isr_common_stub
isr20:
    push byte 0
    push byte 20
    jmp isr_common_stub
isr21:
    push byte 0
    push byte 21
    jmp isr_common_stub
isr22:
    push byte 0
    push byte 22
    jmp isr_common_stub
isr23:
    push byte 0
    push byte 23
    jmp isr_common_stub
isr24:
    push byte 0
    push byte 24
    jmp isr_common_stub
isr25:
    push byte 0
    push byte 25
    jmp isr_common_stub
isr26:
    push byte 0
    push byte 26
    jmp isr_common_stub
isr27:
    push byte 0
    push byte 27
    jmp isr_common_stub
isr28:
    push byte 0
    push byte 28
    jmp isr_common_stub
isr29:
    push byte 0
    push byte 29
    jmp isr_common_stub
isr30:
    push byte 0
    push byte 30
    jmp isr_common_stub
isr31:
    push byte 0
    push byte 31
    jmp isr_common_stub



irq0:
    push byte 0 ; 只是指令中的立即数是1字节 实际入栈还是4字节
    push byte 32
    jmp irq_common_stub
irq1:
    push byte 0
    push byte 33
    jmp irq_common_stub
irq2:
    push byte 0
    push byte 34
    jmp irq_common_stub
irq3:
    push byte 0
    push byte 35
    jmp irq_common_stub
irq4:
    push byte 0
    push byte 36
    jmp irq_common_stub
irq5:
    push byte 0
    push byte 37
    jmp irq_common_stub
irq6:
    push byte 0
    push byte 38
    jmp irq_common_stub
irq7:
    push byte 0
    push byte 39
    jmp irq_common_stub
irq8:
    push byte 0
    push byte 40
    jmp irq_common_stub
irq9:
    push byte 0
    push byte 41
    jmp irq_common_stub
irq10:
    push byte 0
    push byte 42
    jmp irq_common_stub
irq11:
    push byte 0
    push byte 43
    jmp irq_common_stub
irq12:
    push byte 0
    push byte 44
    jmp irq_common_stub
irq13:
    push byte 0
    push byte 45
    jmp irq_common_stub
irq14:
    push byte 0
    push byte 46
    jmp irq_common_stub
irq15:
    push byte 0
    push byte 47
    jmp irq_common_stub
